package com.shengwei.truck.driver.utils;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.view.ui.MainActivity;

public class NotificationUtils extends ContextWrapper {

    private NotificationManager mManager;
    public static final String ANDROID_CHANNEL_ID = "com.baidu.baidulocationdemo";
    public static final String ANDROID_CHANNEL_NAME = "ANDROID CHANNEL";

    @RequiresApi(api = Build.VERSION_CODES.O)
    public NotificationUtils(Context base) {
        super(base);
        createChannels();
    }


    public NotificationUtils(Context base, boolean isAndroid7) {
        super(base);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createChannels() {
        // create android channel
        NotificationChannel androidChannel = new NotificationChannel(ANDROID_CHANNEL_ID,
                ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        androidChannel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        androidChannel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        androidChannel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(androidChannel);
    }

    private NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    /**
     * Android O及以上系统创建通知builder
     * @param title: 通知栏标题
     * @param contentText： 通知栏内容
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getAndroidChannelNotification(String title, String contentText) {
        Intent nfIntent = new Intent(getApplicationContext(), MainActivity.class);
        Notification.Builder builder = new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID);
        builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, nfIntent, 0)) // 设置PendingIntent
                .setContentTitle(title)
                //.setContentText(contentText)
                //.setSubText("SUB")
                //.setTicker("TICKER")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);
        return builder;
    }

    /**
     * Android O以下创建通知builder
     * @param title: 通知栏标题
     * @param contentText： 通知栏内容
     * @return
     */
    public  Notification.Builder getAndroidNotification(String title,String contentText) {
        //获取一个Notification构造器
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        Intent nfIntent = new Intent(getApplicationContext(), MainActivity.class);

        builder.setContentIntent(PendingIntent.
                getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setContentTitle(title) // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
        return builder;
    }
}